import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/login/views/login_buttons.dart';
import 'package:honeypotflutter/modules/login/views/login_protocol.dart';
import 'package:honeypotflutter/modules/login/views/login_textInput.dart';
import 'package:honeypotflutter/modules/login/views/login_timer.dart';
import 'package:honeypotflutter/modules/login/views/login_title.dart';
import 'package:honeypotflutter/public/public_untilController.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_button.dart';
import 'package:honeypotflutter/public/views/public_appBar.dart';
import 'package:honeypotflutter/public/views/public_scrollView.dart';
import 'package:honeypotflutter/public/views/public_tabBar.dart';
import 'logic.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMixin {
  final _logic = Get.put(LoginLogic());
  final HLUntilController _untilController = HLUntilController();
  final FocusNode _phoneNode = FocusNode();
  final FocusNode _pwdNode = FocusNode();
  final FocusNode _codeNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: HLAppBar(
            showBackLeading: false,
            leftWidget: InkWell(
              child: Image.asset("images/logs/logs_delete.png", width: 25, height: 25),
              onTap: _logic.showAlert,
            )),
        backgroundColor: MGColors.itemColor,
        body: HLScrollView(
          padding: EdgeInsets.only(left: 30, right: 30, top: 10),
          nodeList: [_phoneNode, _pwdNode, _codeNode],
          children: [
            LoginTitle(title: "欢迎登录!"),
            SizedBox(height: 10),
            Container(
              child: HLTabBar(
                  menuTitles: ["密码登录", "短信登录"],
                  action: (int index) {
                    _logic.tabBarSelect(index);
                  }),
              width: 145,
            ),
            LoginTextInput(
              hint: "请输入用手机号",
              node: _phoneNode,
              controller: _logic.phoneController,
              keyboardType: TextInputType.phone,
            ),
            Obx(() {
              return _getPwdAndCodeWidget();
            }),
            LoginProtocol(),
            Obx(() {
              return MGButton(
                title: "登录",
                padding: EdgeInsets.only(top: 20),
                enable: _logic.isEnable.value,
                buttonAction: _logic.loginClick,
              );
            }),
            LoginButtons()
          ],
        ));
  }

  Widget _getPwdAndCodeWidget() {
    if (_logic.isPwdLogin.value) {
      return LoginTextInput(
        hint: "请输入密码",
        isPassWord: true,
        node: _pwdNode,
        controller: _logic.pwdController,
      );
    }
    return LoginTimer(
        untilController: _untilController,
        controller: _logic.codeController,
        node: _codeNode,
        codeAction: () {
          if (_logic.isPhoneNumber()) {
            (_untilController.action != null) && _untilController.action!();
            _logic.sendMessage();
          }
        });
  }

  @override
  void dispose() {
    Get.delete<LoginLogic>();
    super.dispose();
  }
}
